﻿package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Vector3D;
	import flash.geom.Point;
	
	public class CubeControl extends Sprite {
		
		private static const BACKGROUND_COLOR = 0xFFFFFF;
		
		private var parentSprite:Sprite;
		private var backgroundArea:Sprite;
		private var backgroundWidth:int;
		private var backGroundHeight:int;
		
		private var cubeDisplay:CubeDisplay;
		private var cubeCentre:Vector3D;
		
		private var doRotate:Boolean = false;
		private var prevX:Number;
		private var prevY:Number;
		private var velX:Number;
		private var velY:Number;
		private var velMag:Number;
		
		
		public function CubeControl(parentSprite:Sprite, backgroundWidth:int, backGroundHeight:int) {
			
			this.parentSprite = parentSprite;	
			this.backgroundWidth = backgroundWidth;
			this.backGroundHeight = backGroundHeight;
			
			backgroundArea = new Sprite();
			parentSprite.addChild(backgroundArea);
			drawBackground();
			
			cubeDisplay = new CubeDisplay(backgroundArea);
			//this.addChild(cubeDisplay); // added this
			cubeDisplay.rotateCube(0,0,0);
			
			cubeCentre = new Vector3D(0.0, 0.0, 0.0);
			
			velX = 1;
			velY = 2;
			velMag = 5;
			setMouseListeners();
			
			this.addEventListener(Event.ENTER_FRAME,whenEnterFrame);
			
		}
		
		public function getVerticeAtIndex(index:uint):Vector3D {
			return cubeDisplay.getVerticeAtIndex(index);
		}
		
		public function drawBackground() {
			backgroundArea.graphics.beginFill(BACKGROUND_COLOR);
			backgroundArea.graphics.drawRect(-(backgroundWidth / 2), -(backGroundHeight / 2), backgroundWidth, backGroundHeight);
			backgroundArea.graphics.endFill();
		}
		
		public function setMouseListeners():void {
			parentSprite.addEventListener(MouseEvent.ROLL_OUT, boardOut);
			parentSprite.addEventListener(MouseEvent.MOUSE_MOVE, boardMove);
			parentSprite.addEventListener(MouseEvent.MOUSE_DOWN, boardDown);
			parentSprite.addEventListener(MouseEvent.MOUSE_UP, boardUp);
		}
		
		// Event functions
		public function whenEnterFrame(e:Event):void {
			if(!doRotate && velMag > 0) {
				cubeDisplay.rotateAndDrawCube(velY,velX,0);
			}
			
			// Test code for setting the location of the link box
			var firstVertice:Vector3D = cubeDisplay.getVerticeAtIndex(0);
			var link:Vector3D = firstVertice.subtract(cubeCentre);
			link.scaleBy(1.2);
			link = firstVertice.add(link);
			
			var firstCoord:Point = new Point();
			firstCoord.x = firstVertice.x;
			firstCoord.y = firstVertice.y;
		}
		
		public function boardOut(e:MouseEvent):void {
			doRotate = false;
		}

		public function boardDown(e:MouseEvent):void {
			prevX = parentSprite.mouseX;
			prevY = parentSprite.mouseY;
			
			velX=0;
			velY=0;
			velMag=0;
			
			doRotate = true;
		}
		

		public function boardUp(e:MouseEvent):void {
			doRotate = false;
		}

		public function boardMove(e:MouseEvent):void {
			var locX:Number = prevX;
			var locY:Number = prevY;

			if (doRotate) {
				prevX = parentSprite.mouseX;
				prevY = parentSprite.mouseY;
				
				// calculate the velocity for AR
				velX=-2*(prevX-locX);
				velY=2*(prevY-locY);
				velMag=Math.abs(velX)+Math.abs(velY);
				
				cubeDisplay.rotateAndDrawCube(prevY-locY,-(prevX-locX),0);
				e.updateAfterEvent();
			}
		}
		
	}
	
}